const { getHero } = require('../dota2api/index')

// 是否赢得比赛
function isWin(playSlot, radiantWin) {
  return [0, 1, 2, 3, 4].includes(playSlot) === radiantWin
}

// 是否天梯
function isLadder(lobbyType) {
  return lobbyType === 7
}

// 是否组排
function isParty(partySize) {
  return partySize > 1
}

// 获取英雄中文名
async function getHeroName(heroId) {
  return new Promise(async (resolve, reject) => {
    try {
      if (heroMap[heroId]) {
        resolve(heroMap[heroId])
      } else {
        const heroList = await getHero()
        const currentHero = heroList.find(item => {
          return item.id === heroId
        })
        resolve(currentHero.localized_name)
      }
    } catch (error) {
      reject(error)
    }
  })
}

// 获取段位信息
function getRankTier(rankTier, leaderboard_rank = '') {
  return new Promise((resolve, reject) => {
    try {
      const tier = tierMap[Number(rankTier.toString()[0])]
      const level = levelMap[Number(rankTier.toString()[1])]
      let tierLevel
      if (Number(rankTier.toString()[0]) === 8) {
        tierLevel = `【${tier}${leaderboard_rank}】`
      } else {
        tierLevel = `【${tier}${level}】`
      }
      resolve(tierLevel)
    } catch (error) {
      reject(error)
    }
  })
}

// 获取英雄中文名
function getHeroCnName(heroId) {
  return (heroMap[heroId]) || '未知英雄'
}

// 获取游戏模式
function getGameMode(game_mode) {
  return gameModeMap[game_mode] || '未知模式'
}

// 获取游戏类型
function getLobbyType(lobby_type) {
  return lobbyTypeMap[lobby_type] || '未知'
}

// 获取队伍类型
function getPartSize(party_size) {
  return partySizeMap[party_size] || '未知'
}

const heroMap = {
  1: '敌法师',
  2: '斧王',
  3: '痛苦之源',
  4: '血魔',
  5: '水晶室女',
  6: '卓尔游侠',
  7: '撼地者',
  8: '主宰',
  9: '米拉娜',
  10: '变体精灵',
  11: '影魔',
  12: '幻影长矛手',
  13: '帕克',
  14: '帕吉',
  15: '剃刀',
  16: '沙王',
  17: '风暴之灵',
  18: '斯温',
  19: '小小',
  20: '复仇之魂',
  21: '风行者',
  22: '宙斯',
  23: '昆卡',
  25: '丽娜',
  26: '莱恩',
  27: '暗影萨满',
  28: '斯拉达',
  29: '潮汐猎人',
  30: '巫医',
  31: '巫妖',
  32: '力丸',
  33: '谜团',
  34: '地精修补匠',
  35: '狙击手',
  36: '瘟疫法师',
  37: '术士',
  38: '兽王',
  39: '痛苦女王',
  40: '剧毒术士',
  41: '虚空假面',
  42: '骷髅王',
  43: '死亡先知',
  44: '幻影刺客',
  45: '帕格纳',
  46: '圣堂刺客',
  47: '毒龙',
  48: '露娜',
  49: '龙骑士',
  50: '戴泽',
  51: '发条地精',
  52: '受折磨的灵魂',
  53: '先知',
  54: '噬魂者',
  55: '黑暗贤者',
  56: '克林克兹',
  57: '全能骑士',
  58: '魅惑魔女',
  59: '哈斯卡',
  60: '暗夜魔王',
  61: '育母蜘蛛',
  62: '赏金猎人',
  63: '编织者',
  64: '双头龙',
  65: '蝙蝠骑士',
  66: '陈',
  67: '幽鬼',
  68: '远古冰魂',
  69: '末日使者',
  70: '拍拍熊',
  71: '裂魂人',
  72: '矮人直升机',
  73: '炼金术士',
  74: '祈求者',
  75: '沉默术士',
  76: '殁境神蚀者',
  77: '狼人',
  78: '酒仙',
  79: '暗影恶魔',
  80: '德鲁伊',
  81: '混沌骑士',
  82: '米波',
  83: '树精卫士',
  84: '食人魔法师',
  85: '不朽尸王',
  86: '拉比克',
  87: '干扰者',
  88: '司夜刺客',
  89: '娜迦海妖',
  90: '光之守卫',
  91: '艾欧',
  92: '维萨吉',
  93: '斯拉克',
  94: '美杜莎',
  95: '巨魔战将',
  96: '半人马战行者',
  97: '马格纳斯',
  98: '伐木机',
  99: '钢背兽',
  100: '巨牙海民',
  101: '天怒法师',
  102: '亚巴顿',
  103: '上古巨神',
  104: '军团指挥官',
  105: '工程师',
  106: '灰烬之灵',
  107: '大地之灵',
  108: '孽主',
  109: '恐怖利刃',
  110: '凤凰',
  111: '神谕者',
  112: '寒冬飞龙',
  113: '天穹守望者',
  114: '齐天大圣',
  119: '邪影芳灵',
  120: '石鳞剑士',
  121: '天涯墨客',
  123: '森海飞霞',
  126: '虚无之灵',
  128: '电炎绝手',
  129: '马尔斯',
  135: '破晓辰星'
}

const gameModeMap = {
  1: '全英雄选择',
  2: '队长模式',
  3: '随机征召',
  4: '单一征召',
  18: '技能征召',
  22: '全英雄选择（BP）'
}

const lobbyTypeMap = {
  0: '普通',
  1: '练习赛',
  2: '锦标赛',
  3: '教程',
  4: '合作对抗机器人',
  5: '天体组队匹配（旧有）',
  6: '天梯单排（旧有）',
  7: '天梯',
  8: '中路对单',
  9: '勇士联赛'
}

const partySizeMap = {
  1: '单排',
  2: '双排',
  3: '三排',
  4: '四黑',
  5: '五黑'
}

const tierMap = {
  0: '未知',
  1: '卫士',
  2: '先锋',
  3: '中军',
  4: '统帅',
  5: '传奇',
  6: '万古流芳',
  7: '超凡入圣',
  8: '冠绝一世'
}

const levelMap = {
  0: '',
  1: 'Ⅰ',
  2: 'Ⅱ',
  3: 'Ⅲ',
  4: 'Ⅳ',
  5: 'Ⅴ',
  6: 'Ⅵ',
  7: 'Ⅶ'
}

module.exports = {
  isWin,
  isLadder,
  isParty,
  getHeroName,
  getHeroCnName,
  getGameMode,
  getLobbyType,
  getPartSize,
  getRankTier
}